package my.mypool.core.thread;



import my.mypool.core.PoolableParent;

public class PoolableThread extends PoolableParent implements Runnable {
    private Object lock = new Object();
    private Taskable task = null;
    private Thread thread = null;
    private boolean isRunning = false;
    public void destroy() throws Exception {
    //	isRunning = false;
    	//System.out.println("destroy");
    	//thread.interrupt();
    	
    }

    public boolean validate() {
        return true;
    }

    public void activate() throws Exception {

    }

    public void passivate() throws Exception {

    }

    public void init(Object param) throws Exception {
    	thread = new Thread(this);
    	isRunning = true;
    	thread.start();
    }

    public void runTask(Taskable task) throws Exception {
        this.task = task;
        synchronized (lock) {
            lock.notifyAll();
        }
    }

    public void run() {
        while (true) {
        	System.out.println("isRunning:" + isRunning);
        	if (isRunning == false) {
				break;
			}
            try {
                if (task != null) {
                    task.doTask();
                }
                synchronized (lock) {
                    lock.wait(0);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
